import java.util.*;

public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static TreeNode init(List<Integer> list) {
        if (list.isEmpty()) {
            return null;
        }
        List<TreeNode> nodeList = new ArrayList<>(list.size());
        TreeNode root = new TreeNode(list.get(0));
        nodeList.add(root);
        for (int i = 1; i < list.size(); i++) {
            Integer val = list.get(i);
            TreeNode node = val != null ? new TreeNode(val) : null;
            nodeList.add(node);
        }
        for (int i = 0; i * 2 + 2 <= list.size(); i++) {
            TreeNode node = nodeList.get(i);
            if (node != null) {
                if (i * 2 + 1 < list.size()) {
                    node.left = nodeList.get(i * 2 + 1);
                }
                if (i * 2 + 2 < list.size()) {
                    node.right = nodeList.get(i * 2 + 2);
                }
            }
        }
        return root;
    }


}